package com.feng.springcloud.controller;

import com.feng.springcloud.entities.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptController_Consumer {
//    private static final String REST_URL_PREFIX = "http://localhost:8001";
    //java.lang.IllegalStateException: Request URI does not contain a valid hostname: /MICROSERVICECLOUD-DEPT/dept/get/1
//    private static final String REST_URL_PREFIX = "MICROSERVICECLOUD-DEPT";
    /**
     * http://localhost/consumer/dept/list
     2021-12-19 17:10:58.676 ERROR 4008 --- [p-nio-80-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: No instances available for MICROSERVICECLOUD-DEPT] with root cause
     java.lang.IllegalStateException: No instances available for MICROSERVICECLOUD-DEPT
     */
    private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD-DEPT";
//    private static final String REST_URL_PREFIX = "http://microservicecloud-dept";
//    private static final String REST_URL_PREFIX = "http://spring-cloud-eureka-server";

    @Autowired
    private RestTemplate restTemplate;
    //https://blog.csdn.net/xuhaogang3/article/details/106356747
//    private RestOperations restTemplate;

    @RequestMapping(value = "/consumer/dept/add")
    public boolean add(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,boolean.class);
    }

    @RequestMapping(value = "/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id")Long id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id, Dept.class);
    }

    @RequestMapping(value = "/consumer/dept/list")
    public List<Dept> deptList(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list", List.class);
    }

    @GetMapping("/hello")
    public String test80(){
        return "hi, 80端口的服务器启动成功了，可以成功访问。";
    }

}
